#include"worker.h"

int saveFile(int netfd, userInfo_t *userInfo, threadPool_t *pthreadPool)
{
    int si =-1;
    recvInfo(netfd,&si);
    if (si==si_failure)
    {
        return 0;
    }
    char md5[64] = {0};
    recvInfo(netfd, md5);
    char sql[256] = {0};
    sprintf(sql, "SELECT size,owner FROM ServerDir WHERE md5='%s'&& type=2", md5);
    puts(sql);
    m_MysqlQuery(pthreadPool->mysql, sql);
    MYSQL_RES *results = mysql_store_result(pthreadPool->mysql);
    char filename[64]={0};
    int filesize=0;
    if (results->row_count == 0) // 文件不存在就要重新创建
    {
        int si = 0;
        sendInfo(netfd, &si, sizeof(si));
        //接收新的文件
        filesize=recvFile(netfd, "./fileLib",md5);
        char filePath[128] = {0};
        sprintf(filePath, "./fileLib/%s", md5);
        unsigned char MD[64]={0};
        char MDStr[64]={0};
        GenFileMD(filePath, MD);
        MDtoStr(MD,MDStr);
        mysql_free_result(results);
        if(strcmp(MDStr,md5)!=0){
            int si = si_failure;
            sendInfo(netfd, &si, sizeof(si));
            return -1;
        }
    }
    else // 文件存在,仅仅构造信息行，这里对文件存在的位置和所属没有区分
    {
        int si = 1;
        sendInfo(netfd, &si, sizeof(si));
        MYSQL_ROW row;
        row = mysql_fetch_row(results);
        //printf("row:%s\n", row[0]);
        filesize=atoi(row[0]);
    }
    recvFilename(netfd,filename);
    // puts(filename);
    // puts(userInfo->username);
    time_t timer;
    struct tm *Now;
    time(&timer);
    Now = localtime(&timer);
    bzero(sql, sizeof(sql));
    sprintf(sql, "INSERT INTO ServerDir VALUE('%s','%s',2,%d,%d,'%s','%d-%02d-%02d %02d:%02d:%02d',NULL,NULL)",
            filename,
            userInfo->username,
            filesize,
            userInfo->pos,
            md5,
            Now->tm_year + 1900,
            Now->tm_mon + 1,
            Now->tm_mday,
            Now->tm_hour,
            Now->tm_min,
            Now->tm_sec
            );
    puts(sql);
    m_MysqlQuery(pthreadPool->mysql, sql);
    mysql_free_result(results);
    si = si_success;
    sendInfo(netfd, &si, sizeof(si));
}

int downloadFile(int netfd, userInfo_t *userInfo, threadPool_t *pthreadPool)
{
    int si =-1;
    recvInfo(netfd,&si);
    if (si==si_failure)
    {
        return 0;
    }
    char filename[128]={0};
    recvFilename(netfd,filename);
    char sql[256] = {0};
    sprintf(sql, "SELECT md5 FROM ServerDir WHERE name='%s'", filename);
    puts(sql);
    m_MysqlQuery(pthreadPool->mysql, sql);
    MYSQL_RES *results = mysql_store_result(pthreadPool->mysql);
    if (results->row_count == 0) // 文件不存在
    {
        int si = si_failure;
        sendInfo(netfd, &si, sizeof(si));
        mysql_free_result(results);
        return -1;
    }
    else // 文件存在,发送文件
    {
        int si = 1;
        sendInfo(netfd, &si, sizeof(si));
        MYSQL_ROW row;
        row = mysql_fetch_row(results);
        char md5[64]={0};
        strcpy(md5,row[0]);
        sendInfo(netfd,md5,strlen(md5));

        char filePath[128] = {0};
        sprintf(filePath, "./fileLib/%s", md5);
        sendFile(netfd,filePath);
        mysql_free_result(results);
    }
    
}

int m_ls(int netfd, userInfo_t *userInfo, threadPool_t *pthreadPool)
{
    char sql[256] = {0};
    sprintf(sql, "SELECT id,name,owner,type,size,parent,dateandtime from ServerDir WHERE parent=%d", userInfo->pos);
    puts(sql);
    m_MysqlQuery(pthreadPool->mysql, sql);
    MYSQL_RES *results = mysql_store_result(pthreadPool->mysql);
    MYSQL_ROW row;
    printf("total rows:%llu\n", mysql_num_rows(results));
    int c = 0;
    char buf[4096] = {0};
    char *p = buf;
    while ((row = mysql_fetch_row(results)) != NULL)
    {
        for (int i = 0; i < mysql_num_fields(results); i++)
        {
            int ret = sprintf(p, "%s\t", row[i]);
            p = p + ret;
        }
        int ret = sprintf(p, "\n");
        p = p + ret;
    }
    // puts(buf);
    sendInfo(netfd, buf, strlen(buf));
    mysql_free_result(results);
}
int m_cd(int netfd, userInfo_t *userInfo, threadPool_t *pthreadPool)
{
    int si = -1;
    recvInfo(netfd, &si);
    if (si)
    {
        char paras[128] = {0};
        recvInfo(netfd, paras);
        if (strcmp(paras, "..") == 0)
        {
            char sql[256] = {0};
            sprintf(sql, "SELECT parent from ServerDir WHERE id=%d && owner = '%s'", userInfo->pos, userInfo->username);
            puts(sql);
            m_MysqlQuery(pthreadPool->mysql, sql);
            MYSQL_RES *results = mysql_store_result(pthreadPool->mysql);
            if (results->row_count == 0)
            {
                int si = si_failure;
                sendInfo(netfd, &si, sizeof(si));
            }
            else
            {

                int si = si_success;
                sendInfo(netfd, &si, sizeof(si));
                MYSQL_ROW row;
                row = mysql_fetch_row(results);
                // printf("row:%s %s\n", row[0], row[1]);
                userInfo->pos = atoi(row[0]);
                printf("userInfo->pos:%d\n", userInfo->pos);
                mysql_free_result(results);
                sendInfo(netfd, userInfo, sizeof(userInfo_t));
            }
            return 0;
        }
        else
        {
            char sql[256] = {0};
            sprintf(sql, "SELECT id from ServerDir WHERE parent=%d && name='%s' && type=1  ", userInfo->pos, paras);
            puts(sql);
            m_MysqlQuery(pthreadPool->mysql, sql);
            MYSQL_RES *results = mysql_store_result(pthreadPool->mysql);
            if (results->row_count == 0) // 不存在
            {
                int si = si_failure;
                sendInfo(netfd, &si, sizeof(si));
                mysql_free_result(results);
                return -1;
            }
            else
            {
                int si = si_success;
                sendInfo(netfd, &si, sizeof(si));
                MYSQL_ROW row;
                row = mysql_fetch_row(results);
                // printf("row:%s %s\n", row[0], row[1]);
                userInfo->pos = atoi(row[0]);
                printf("userInfo->pos:%d\n", userInfo->pos);
                mysql_free_result(results);
                sendInfo(netfd, userInfo, sizeof(userInfo_t));
                return 0;
            }
        }
    }
}

int m_mkdir(int netfd, userInfo_t *userInfo, threadPool_t *pthreadPool)
{
    int si = -1;
    recvInfo(netfd, &si);
    if (si)
    {
        char paras[128] = {0};
        recvInfo(netfd, paras);
        // printf("paras%s\n",paras);
        time_t timer;
        struct tm *Now;
        time(&timer);
        Now = localtime(&timer);
        char sql[256] = {0};
        bzero(sql, sizeof(sql));
        sprintf(sql, "INSERT INTO ServerDir VALUE('%s','%s',1,1,%d,NULL,'%d-%02d-%02d %02d:%02d:%02d',NULL,NULL)",
                paras,
                userInfo->username,
                userInfo->pos,
                Now->tm_year + 1900,
                Now->tm_mon + 1,
                Now->tm_mday,
                Now->tm_hour,
                Now->tm_min,
                Now->tm_sec);
        puts(sql);
        m_MysqlQuery(pthreadPool->mysql, sql);
    }
}

int m_pwd(int netfd, userInfo_t *userInfo, threadPool_t *pthreadPool)
{
    char m_path[256] = {0};
    int c=1;
    int pos= userInfo->pos;
    while (c != 0)
    {
        char sql[256] = {0};
        sprintf(sql, "SELECT name,parent from ServerDir WHERE id=%d && owner = '%s'", pos, userInfo->username);
        puts(sql);
        m_MysqlQuery(pthreadPool->mysql, sql);
        MYSQL_RES *results = mysql_store_result(pthreadPool->mysql);
        c=results->row_count;
        if (c) // 判断是否到根目录
        {
            MYSQL_ROW row;
            row = mysql_fetch_row(results);
            // printf("row:%s %s\n", row[0], row[1]);
            char buf[256]={0};
            strcpy(buf,m_path);
            sprintf(m_path, "%s/%s", row[0], buf);
            pos = atoi(row[1]);
            printf("pos:%d\n", pos);
        }
        mysql_free_result(results);
    }
    puts(m_path);
    sendInfo(netfd, m_path, strlen(m_path));
    return 0;
}
